package com.it.schoolbookshop_back.mapper;

import com.it.schoolbookshop_back.entities.po.User;
import org.apache.ibatis.annotations.*;
import java.math.BigDecimal;

/**
 * 用户DAO层接口
 */
@Mapper
public interface UserMapper {
    
    /**
     * 根据用户名查询用户
     * @param username 用户名
     * @return 用户对象
     */
    User findByUsername(String username);
    
    /**
     * 添加新用户
     * @param user 用户对象
     * @return 影响行数
     */
    int insert(User user);
    
    /**
     * 根据ID查询用户
     * @param id 用户ID
     * @return 用户对象
     */
    User findById(Integer id);
    
    /**
     * 根据用户名和密码查询用户（登录）
     * @param username 用户名
     * @param password 密码
     * @return 用户对象
     */
    User findByUsernameAndPassword(@Param("username") String username, @Param("password") String password);
    
    /**
     * 根据ID获取用户
     * @param id 用户ID
     * @return 用户对象
     */
    @Select("SELECT * FROM users WHERE id = #{id}")
    User getUserById(Integer id);
    
    /**
     * 更新用户信息
     * @param user 用户对象
     * @return 影响的行数
     */
    @Update("UPDATE users SET " +
            "username = #{username}, " +
            "password = #{password}, " +
            "role = #{role}, " +
            "status = #{status}, " +
            "student_id = #{studentId}, " +
            "real_name = #{realName}, " +
            "department = #{department}, " +
            "address = #{address}, " +
            "email = #{email}, " +
            "phone = #{phone}, " +
            "wechat = #{wechat}, " +
            "avatar = #{avatar}, " +
            "avatar_type = #{avatarType}, " +
            "bio = #{bio}, " +
            "email_verified = #{emailVerified}, " +
            "phone_verified = #{phoneVerified}, " +
            "bookCount = #{bookCount}, " +
            "soldCount = #{soldCount}, " +
            "boughtCount = #{boughtCount}, " +
            "favoriteCount = #{favoriteCount}, " +
            "lastLoginAt = #{lastLoginAt}, " +
            "updated_at = NOW() " +
            "WHERE id = #{id}")
    int updateUser(User user);
    
    /**
     * 更新用户余额
     * @param userId 用户ID
     * @param balance 新的余额
     * @return 影响的行数
     */
    int updateUserBalance(@Param("userId") Integer userId, @Param("balance") BigDecimal balance);
    
    /**
     * 获取用户设置
     * @param userId 用户ID
     * @return 用户对象，包含用户设置
     */
    @Select("SELECT * FROM users WHERE id = #{userId}")
    User getUserSettings(@Param("userId") Integer userId);
    
    /**
     * 更新用户设置
     * @param userId 用户ID
     * @param notificationMessages 是否接收消息通知
     * @param notificationFavorites 是否接收收藏通知
     * @param notificationTransactions 是否接收交易通知
     * @param notificationSystem 是否接收系统通知
     * @return 受影响的行数
     */
    @Update("UPDATE users SET " +
            "notification_messages = #{notificationMessages}, " +
            "notification_favorites = #{notificationFavorites}, " +
            "notification_transactions = #{notificationTransactions}, " +
            "notification_system = #{notificationSystem} " +
            "WHERE id = #{userId}")
    int updateUserSettings(
            @Param("userId") Integer userId,
            @Param("notificationMessages") Boolean notificationMessages,
            @Param("notificationFavorites") Boolean notificationFavorites,
            @Param("notificationTransactions") Boolean notificationTransactions,
            @Param("notificationSystem") Boolean notificationSystem);
            
    /**
     * 增加用户在售书籍数量
     * @param userId 用户ID
     * @return 影响的行数
     */
    @Update("UPDATE users SET bookCount = IFNULL(bookCount, 0) + 1 WHERE id = #{userId}")
    int incrementBookCount(@Param("userId") Integer userId);
    
    /**
     * 减少用户在售书籍数量
     * @param userId 用户ID
     * @return 影响的行数
     */
    @Update("UPDATE users SET bookCount = GREATEST(IFNULL(bookCount, 0) - 1, 0) WHERE id = #{userId}")
    int decrementBookCount(@Param("userId") Integer userId);
    
    /**
     * 增加用户售出书籍数量
     * @param userId 用户ID
     * @return 影响的行数
     */
    @Update("UPDATE users SET soldCount = IFNULL(soldCount, 0) + 1 WHERE id = #{userId}")
    int incrementSoldCount(@Param("userId") Integer userId);
    
    /**
     * 增加用户购买书籍数量
     * @param userId 用户ID
     * @return 影响的行数
     */
    @Update("UPDATE users SET boughtCount = IFNULL(boughtCount, 0) + 1 WHERE id = #{userId}")
    int incrementBoughtCount(@Param("userId") Integer userId);
    
    /**
     * 更新用户最后登录时间
     * @param userId 用户ID
     * @return 影响的行数
     */
    @Update("UPDATE users SET lastLoginAt = NOW() WHERE id = #{userId}")
    int updateLastLoginTime(@Param("userId") Integer userId);
} 